/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Dao.AtividadeDao;
import Dao.AtividadeDaoImplJPA;
import Dao.HistoricoTurmaDao;
import Dao.HistoricoTurmaDaoImplJPA;
import Dao.NotaDao;
import Dao.NotaDaoImplJPA;
import Dao.TurmaDao;
import Dao.TurmaDaoImplJPA;
import Pojo.Aluno;
import Pojo.Atividade;
import Pojo.HistoricoTurma;
import Pojo.Nota;
import Pojo.Professor;
import Pojo.Turma;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author thiago
 */
public class FormConsultaNotas extends javax.swing.JInternalFrame {

    /**
     * Acesso necessário para o caso de chamar o cadastro de professore
     */
    private int acesso;
    
    /**
     * Objeto professor para acesso.
     */
    private Professor professorAcesso;
    
    /**
     * Objeto aluno para acesso
     */
    private Aluno alunoAcesso;
    
    /**
     * Getter do acesso
     * @return 
     */
    public int getAcesso() {
        return acesso;
    }
    
    /**
     * Setter de acesso
     * @param acesso 
     */
    private void setAcesso(int acesso){
        this.acesso = acesso;
    }

    public Professor getProfessorAcesso() {
        return professorAcesso;
    }

    private void setProfessorAcesso(Professor professorAcesso) {
        this.professorAcesso = professorAcesso;
    }

    public Aluno getAlunoAcesso() {
        return alunoAcesso;
    }

    private void setAlunoAcesso(Aluno alunoAcesso) {
        this.alunoAcesso = alunoAcesso;
    }
        
    /**
     * Creates new form FormConsultaNotas
     */
    public FormConsultaNotas(int acesso, Professor professor, Aluno aluno) {
        initComponents();
        this.setAcesso(acesso);
        this.setProfessorAcesso(professor);
        this.setAlunoAcesso(aluno);
        this.buscarTurmas();
    }

    private void buscarTurmas(){
        if (acesso==2){
            TurmaDao turmaDao = new TurmaDaoImplJPA();
            List<Turma> listaTurmas = turmaDao.pesquisarPorProfessor(this.getProfessorAcesso());
            for(Turma turma : listaTurmas){
                cbbTurmas.addItem(turma.getId()+" - "+turma.getDisciplina().getNome()+" - "+turma.getAno()+" - "+turma.getPeriodo());
            }
            lblAlunoProfessor.setText("Professor");
            lblAlunoProfessor1.setText(this.getProfessorAcesso().getNome());
        }else if (acesso==3){
            HistoricoTurmaDao historicoTurmaDao = new HistoricoTurmaDaoImplJPA();
            List<HistoricoTurma> listaHistoricoTurmas = historicoTurmaDao.pesquisarPorAluno(this.getAlunoAcesso());
            for(HistoricoTurma historicoTurma : listaHistoricoTurmas){
                Turma turma = historicoTurma.getTurma();
                cbbTurmas.addItem(turma.getId()+" - "+turma.getDisciplina().getNome()+" - "+turma.getAno()+" - "+turma.getPeriodo());
            }
        }
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        lblAlunoProfessor = new javax.swing.JLabel();
        lblAlunoProfessor1 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        cbbTurmas = new javax.swing.JComboBox();
        btnPesquisarTurmas = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        cbbAtividades = new javax.swing.JComboBox();
        btnPesquisarNotas = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        lblTurma = new javax.swing.JLabel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tblNotas = new javax.swing.JTable();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane2.setViewportView(jTable2);

        setClosable(true);
        setTitle("Consulta de Notas");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Pesquisar Atividades"));

        lblAlunoProfessor.setText("Professor");

        jLabel1.setText("Turmas");

        btnPesquisarTurmas.setText("Pesquisar Atividades ");
        btnPesquisarTurmas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarTurmasActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lblAlunoProfessor)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(lblAlunoProfessor1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnPesquisarTurmas, javax.swing.GroupLayout.PREFERRED_SIZE, 187, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(cbbTurmas, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(11, 11, 11)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(cbbTurmas, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(btnPesquisarTurmas)
                    .addComponent(lblAlunoProfessor1, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblAlunoProfessor, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Pesquisar Notas"));

        jLabel2.setText("Atividades");

        btnPesquisarNotas.setText("Pesquisar Notas");
        btnPesquisarNotas.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPesquisarNotasActionPerformed(evt);
            }
        });

        jLabel3.setText("Turma");

        lblTurma.setPreferredSize(new java.awt.Dimension(40, 0));

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(lblTurma, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnPesquisarNotas))
                    .addComponent(cbbAtividades, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(cbbAtividades, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(lblTurma, javax.swing.GroupLayout.PREFERRED_SIZE, 15, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3)
                    .addComponent(btnPesquisarNotas)))
        );

        tblNotas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Aluno", "Atividade", "Nota"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane3.setViewportView(tblNotas);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 285, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnPesquisarTurmasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarTurmasActionPerformed
        String turmaString = (String) cbbTurmas.getSelectedItem();
        if (turmaString.length()>0){
            String[] dados = turmaString.split(" - ");
            TurmaDao turmaDao = new TurmaDaoImplJPA();
            Turma turma = turmaDao.pesquisarPorId(Integer.parseInt(dados[0]));

            AtividadeDao atividadeDao = new AtividadeDaoImplJPA();
            List<Atividade> listaAtividades = atividadeDao.pesquisarPorTurma(turma);
            for(Atividade atividade : listaAtividades){
                cbbAtividades.addItem(atividade.getId()+" - "+atividade.getNome());
            }
            lblTurma.setText(turma.getDisciplina().getNome()+" - "+turma.getAno()+" - "+turma.getPeriodo());
        }else{
            JOptionPane.showMessageDialog(null, "Informe uma turma");
        }
    }//GEN-LAST:event_btnPesquisarTurmasActionPerformed

    private void btnPesquisarNotasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPesquisarNotasActionPerformed
        String atividadeString = (String) cbbAtividades.getSelectedItem();
        String[] dados = atividadeString.split(" - ");
        NotaDao notaDao = new NotaDaoImplJPA();
        AtividadeDao atividadeDao = new AtividadeDaoImplJPA();
        Atividade atividade = atividadeDao.pesquisarPorId(Integer.parseInt(dados[0]));
        List<Nota> listaNotas = null;
        if (acesso==2){
            listaNotas = notaDao.pesquisarPorAtividade(atividade);
        }else if (acesso==3){
            listaNotas = new ArrayList<Nota>();
            Nota nota = notaDao.pesquisarNota(this.getAlunoAcesso(), atividade);
            listaNotas.add(nota);
        }
        DefaultTableModel model = (DefaultTableModel) tblNotas.getModel();
        Object[] linha = new Object[4];
        model.setRowCount(0);
        for (Object o: listaNotas){
            Nota n = (Nota) o;
            linha[0] = String.valueOf(n.getId());
            linha[1] = n.getAluno().getNome();
            linha[2] = n.getAtividade().getNome();
            linha[3] = n.getNota();
            model.addRow(linha);
        }
        tblNotas.repaint();
    }//GEN-LAST:event_btnPesquisarNotasActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnPesquisarNotas;
    private javax.swing.JButton btnPesquisarTurmas;
    private javax.swing.JComboBox cbbAtividades;
    private javax.swing.JComboBox cbbTurmas;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JLabel lblAlunoProfessor;
    private javax.swing.JLabel lblAlunoProfessor1;
    private javax.swing.JLabel lblTurma;
    private javax.swing.JTable tblNotas;
    // End of variables declaration//GEN-END:variables
}
